package com.github.iot.dao;

import android.content.Context;

import com.github.iot.dao.greendao.UserDao;
import com.github.iot.entity.User;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * TODO
 *
 * @Author: 梁声洪
 * @Date: 2019/12/7 13:45
 * @Version: v1.0.0
 */
public class UserDaoUtils {

    private DaoManager mManager;

    public UserDaoUtils(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成用户记录的插入，如果表未创建，先创建用户表
     *
     * @param user
     * @return
     */
    public boolean insertUser(User user) {
        return mManager.getDaoSession().getUserDao().insert(user) == -1 ? false : true;
    }

    /**
     * 插入多条数据，在子线程操作
     *
     * @param lists
     * @return
     */
    public boolean insertUser(final List<User> lists) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(() -> {
                for (User user : lists) {
                    mManager.getDaoSession().insertOrReplace(user);
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param user
     * @return
     */
    public boolean updateUser(User user) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(user);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param user
     * @return
     */
    public boolean deleteUser(User user) {
        boolean flag = false;
        try {
            mManager.getDaoSession().delete(user);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(User.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<User> queryAllUser() {
        return mManager.getDaoSession().loadAll(User.class);
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public User queryUserById(long key) {
        return mManager.getDaoSession().load(User.class, key);
    }

    /**
     * 使用native sql进行查询操作
     *
     * @param sql
     * @param conditions
     * @return
     */
    public List<User> queryUserByNativeSql(String sql, String[] conditions) {
        return mManager.getDaoSession().queryRaw(User.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
    public List<User> queryMeiziByQueryBuilder(long id) {
        QueryBuilder<User> queryBuilder = mManager.getDaoSession().queryBuilder(User.class);
        return queryBuilder.where(UserDao.Properties.Id.eq(id)).list();
    }

}